﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using XUpdater;

namespace XUtliPoolLib
{
	// Token: 0x02000218 RID: 536
	public class XFetchVersionNetwork
	{
		// Token: 0x06000C05 RID: 3077 RVA: 0x0003EAE8 File Offset: 0x0003CCE8
		public XFetchVersionNetwork()
		{
			this.m_oSocket = null;
			this.m_nState = XFetchVersionNetwork.SocketState.State_Closed;
			XFetchVersionNetwork.m_oRecvBuff = null;
			this.m_nCurrRecvLen = 0;
			this.m_RecvCb = new AsyncCallback(this.RecvCallback);
			this.m_ConnectCb = new AsyncCallback(this.OnConnect);
		}

		// Token: 0x06000C06 RID: 3078 RVA: 0x0003EB68 File Offset: 0x0003CD68
		private void GetNetworkType()
		{
			try
			{
				string hostNameOrAddress = XSingleton<XUpdater.XUpdater>.singleton.XPlatform.GetLoginServer("QQ").Substring(0, XSingleton<XUpdater.XUpdater>.singleton.XPlatform.GetLoginServer("QQ").LastIndexOf(':'));
				IPAddress[] hostAddresses = Dns.GetHostAddresses(hostNameOrAddress);
				this.m_NetworkType = hostAddresses[0].AddressFamily;
			}
			catch (Exception ex)
			{
				XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
			}
		}

		// Token: 0x06000C07 RID: 3079 RVA: 0x0003EBF0 File Offset: 0x0003CDF0
		public bool Init()
		{
			this.GetNetworkType();
			try
			{
				this.m_nState = XFetchVersionNetwork.SocketState.State_Closed;
				this.m_oSocket = new Socket(this.m_NetworkType, SocketType.Stream, ProtocolType.Tcp);
				this.m_oSocket.NoDelay = true;
				if (XFetchVersionNetwork.m_oRecvBuff == null)
				{
					XFetchVersionNetwork.m_oRecvBuff = new byte[512];
				}
				this.m_nCurrRecvLen = 0;
			}
			catch (Exception ex)
			{
				XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, "new Socket Error!", null, null, null, null);
				return false;
			}
			return true;
		}

		// Token: 0x06000C08 RID: 3080 RVA: 0x0003EC88 File Offset: 0x0003CE88
		public void UnInit()
		{
			this.Close();
		}

		// Token: 0x06000C09 RID: 3081 RVA: 0x0003EC94 File Offset: 0x0003CE94
		private void OnConnect(IAsyncResult iar)
		{
			try
			{
				if (this.m_nState != XFetchVersionNetwork.SocketState.State_Closed)
				{
					Socket socket = (Socket)iar.AsyncState;
					socket.EndConnect(iar);
					this.SetState(XFetchVersionNetwork.SocketState.State_Connected);
					socket.BeginReceive(XFetchVersionNetwork.m_oRecvBuff, this.m_nCurrRecvLen, XFetchVersionNetwork.m_oRecvBuff.Length - this.m_nCurrRecvLen, SocketFlags.None, this.m_RecvCb, socket);
				}
			}
			catch (Exception ex)
			{
				XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
				this.SetState(XFetchVersionNetwork.SocketState.State_Closed);
				this.Close();
			}
		}

		// Token: 0x06000C0A RID: 3082 RVA: 0x0003ED34 File Offset: 0x0003CF34
		public bool Connect(string host, int port)
		{
			bool result;
			try
			{
				XSingleton<XDebug>.singleton.AddLog("Connect...ip:" + host + " port" + port, null, null, null, null, null, XDebugColor.XDebug_None);
				this.SetState(XFetchVersionNetwork.SocketState.State_Connecting);
				this.m_oSocket.BeginConnect(host, port, this.m_ConnectCb, this.m_oSocket);
				result = true;
			}
			catch (Exception ex)
			{
				XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
				result = false;
			}
			return result;
		}

		// Token: 0x06000C0B RID: 3083 RVA: 0x0003ED98 File Offset: 0x0003CF98
		public void Close()
		{
			this.m_nState = XFetchVersionNetwork.SocketState.State_Closed;
			if (this.m_oSocket != null)
			{
				try
				{
					this.m_oSocket.Close();
				}
				catch (Exception ex)
				{
					XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
				}
				this.m_oSocket = null;
			}
		}

		// Token: 0x06000C0C RID: 3084 RVA: 0x0003EE00 File Offset: 0x0003D000
		public Socket GetSocket()
		{
			return this.m_oSocket;
		}

		// Token: 0x06000C0D RID: 3085 RVA: 0x0003EE18 File Offset: 0x0003D018
		public bool IsClosed()
		{
			return this.m_nState == XFetchVersionNetwork.SocketState.State_Closed;
		}

		// Token: 0x06000C0E RID: 3086 RVA: 0x0003EE33 File Offset: 0x0003D033
		private void SetState(XFetchVersionNetwork.SocketState nState)
		{
			this.m_nState = nState;
		}

		// Token: 0x06000C0F RID: 3087 RVA: 0x0003EE40 File Offset: 0x0003D040
		public void RecvCallback(IAsyncResult ar)
		{
			try
			{
				if (this.m_nState != XFetchVersionNetwork.SocketState.State_Closed)
				{
					Socket socket = (Socket)ar.AsyncState;
					int num = socket.EndReceive(ar);
					if (num > 0)
					{
						XFetchVersionNetwork.TotalRecvBytes += num;
						this.m_nCurrRecvLen += num;
						if (!this.DetectPacket())
						{
							if (this.m_nCurrRecvLen == XFetchVersionNetwork.m_oRecvBuff.Length)
							{
								XSingleton<XDebug>.singleton.AddWarningLog("RecvCallback error ! m_nCurrRecvLen == m_oRecvBuff.Length", null, null, null, null, null);
							}
							socket.BeginReceive(XFetchVersionNetwork.m_oRecvBuff, this.m_nCurrRecvLen, XFetchVersionNetwork.m_oRecvBuff.Length - this.m_nCurrRecvLen, SocketFlags.None, this.m_RecvCb, socket);
						}
					}
					else
					{
						if (num == 0)
						{
							XSingleton<XDebug>.singleton.AddWarningLog("Close socket normally", null, null, null, null, null);
							this.Close();
						}
						else
						{
							XSingleton<XDebug>.singleton.AddWarningLog("Close socket, recv error!", null, null, null, null, null);
							this.Close();
						}
					}
				}
			}
			catch (ObjectDisposedException)
			{
			}
			catch (SocketException ex)
			{
				XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
				this.Close();
			}
			catch (Exception ex2)
			{
				XSingleton<XDebug>.singleton.AddWarningLog(ex2.Message, null, null, null, null, null);
				this.Close();
			}
		}

		// Token: 0x06000C10 RID: 3088 RVA: 0x0003EFE8 File Offset: 0x0003D1E8
		public bool DetectPacket()
		{
			if (this.m_nCurrRecvLen > 0)
			{
				int num = this.BreakPacket(XFetchVersionNetwork.m_oRecvBuff, 0, this.m_nCurrRecvLen);
				if (num != 0)
				{
					if (this.m_bRecvMsg)
					{
						byte[] array = new byte[num];
						Array.Copy(XFetchVersionNetwork.m_oRecvBuff, 0, array, 0, num);
						Encoding utf = Encoding.UTF8;
						XSingleton<XUpdater.XUpdater>.singleton.SetServerVersion(utf.GetString(array, 4, num - 4));
						return true;
					}
				}
			}
			return false;
		}

		// Token: 0x06000C11 RID: 3089 RVA: 0x0003F074 File Offset: 0x0003D274
		public int BreakPacket(byte[] data, int index, int len)
		{
			int result;
			if (len < 4)
			{
				result = 0;
			}
			else
			{
				int num = BitConverter.ToInt32(data, index);
				if (len < 4 + num)
				{
					result = 0;
				}
				else
				{
					result = num + 4;
				}
			}
			return result;
		}

		// Token: 0x04000746 RID: 1862
		private Socket m_oSocket;

		// Token: 0x04000747 RID: 1863
		private XFetchVersionNetwork.SocketState m_nState;

		// Token: 0x04000748 RID: 1864
		private static byte[] m_oRecvBuff;

		// Token: 0x04000749 RID: 1865
		private int m_nCurrRecvLen;

		// Token: 0x0400074A RID: 1866
		public static int TotalRecvBytes;

		// Token: 0x0400074B RID: 1867
		private AsyncCallback m_RecvCb = null;

		// Token: 0x0400074C RID: 1868
		private AsyncCallback m_ConnectCb = null;

		// Token: 0x0400074D RID: 1869
		public bool m_bRecvMsg = true;

		// Token: 0x0400074E RID: 1870
		public bool m_bPause = false;

		// Token: 0x0400074F RID: 1871
		public int m_nPauseRecvLen = 0;

		// Token: 0x04000750 RID: 1872
		private AddressFamily m_NetworkType = AddressFamily.InterNetwork;

		// Token: 0x020003A8 RID: 936
		public enum SocketState
		{
			// Token: 0x0400105F RID: 4191
			State_Closed,
			// Token: 0x04001060 RID: 4192
			State_Connecting,
			// Token: 0x04001061 RID: 4193
			State_Connected
		}
	}
}
